package cc.linker.steplink.crm.controller.form;

import cc.linker.steplink.crm.commons.Constants;
import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.commons.form.FormRequest;
import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.commons.model.SystemProperties;
import cc.linker.steplink.crm.merchanth5.commons.FormFilter;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.dao.PreciseDao;
import cc.linker.steplink.crm.merchanth5.pojo.form.ExFormAttrPo;
import cc.linker.steplink.crm.commons.model.FormLoginDto;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.service.form.FormService;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: CKQ
 * @DateTime: 2020/8/6 9:27
 * @Description: TODO
 */
@Slf4j
@RestController
public class FormController {
    @Autowired
    private FormService formService;
    @Autowired
    private FormRequest formRequest;
    @Autowired
    private FormFilter formFilter;

    /**
     * @Description：表单中转接口
     * @DateTime：2020/10/20 11:26
     * @Params：
     * @Return：
     */
    @PostMapping("/form/common")
    public Object commonRequest(HttpServletRequest request, @RequestBody Map<String, Object> params) {
        FormLoginDto formLoginDto = formFilter.getFormLoginDto(request, false);
        return formRequest.doRequest(params.get("active").toString(), params, formLoginDto);
//        if("/pc/form/templates/getList".equals(params.get("active").toString())){
//            if(res!=null){
//                Map<String,Object> resmap = (Map<String,Object>)res;
//                List<Map<String,Object>> list = (List<Map<String,Object>>)resmap.get("list");
//                if(list!=null && list.size()>0){
//                    for(Map<String,Object> da:list){
//                        String formId = da.get("formId").toString();
//                        //TODO 获取第三方销量
//                        da.put("otherSaleNum",0);
//                    }
//                }
//            }
//        }
//        return res;
    }


    /**
     * @Description：添加日志行为
     * @DateTime：2020/10/27 13:22
     * @Params：
     * @Return：
     */
    @PostMapping("/h5/form/log/add")
    public void addLog(@RequestParam(defaultValue = "",required = false) Long formId,
                       @RequestParam(defaultValue = "",required = true) String formTitle,
                       @RequestParam(defaultValue = "",required = false) String customerId,
                       @RequestParam(defaultValue = "",required = false) String appId,
                       @RequestParam(defaultValue = "",required = false) String orgId,
                       @RequestParam(defaultValue = "1",required = false) Integer type,
                       @RequestParam(defaultValue = "0",required = false) Integer second,
                       @RequestParam(defaultValue = "0",required = false) String amount,
                       HttpServletRequest request) {
        FormLoginDto formLoginDto = formFilter.getFormLoginDto(request, true);
        Map<String, Object> params = new HashMap<>();
        params.put("formId", formId);
        params.put("formTitle", formTitle);
        params.put("customerId", customerId);
        params.put("appId", appId);
        params.put("orgId", orgId);
        params.put("type", type);
        params.put("second", second);
        params.put("amount", amount);
        this.addLog(params,formLoginDto);
    }

    /**
     * @Description：异步记录日志
     * @DateTime：2020/10/26 8:50
     * @Params：
     * @Return：
     */
    @Async
    protected void addLog(Map<String, Object> params,FormLoginDto formLoginDto) {
        this.formRequest.doRequest("/h5/form/log/add", params, formLoginDto);
    }

    /**
     * @Description：表单中转接口
     * @DateTime：2020/10/20 11:26
     * @Params：
     * @Return：
     */
    @PostMapping("/h5/form/common")
    public Object h5CommonRequest(HttpServletRequest request, @RequestBody Map<String, Object> params) {
        FormLoginDto formLoginDto = formFilter.getFormLoginDto(request, true);
        return formRequest.doRequest(params.get("active").toString(), params, formLoginDto);
    }

    /**
     * @Description：通过应用ID获取题目和选项列表
     * @DateTime：2020/8/11 15:22
     * @Params：
     * @Return：
     */
    @GetMapping("/pc/form/getFormAttrList")
    public List<ExFormAttrPo> getFormAttrListByAppId(HttpServletRequest request,
                                                     @RequestParam(defaultValue = "", required = false) String formId) {
        //验证应用信息是否符合规则
        return this.formService.getFormAttrListByAppId(formId);
    }

}
